package com.water.camera.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.water.camera.bean.MediaData;
import com.water.camera.databinding.ActivityDmEditPhotoBinding;
import com.water.camera.fragment.BaseDialogFragment;
import com.water.camera.fragment.MarkDlg;
import com.water.camera.it.WaterMarkViewChangeListener;
import com.water.camera.mark.BaseMark;
import com.water.camera.util.SaveUtils;
import com.water.camera.util.ToastUtil;
import com.water.camera.util.Utils;
import com.xiaopo.flying.sticker.BitmapStickerIcon;
import com.xiaopo.flying.sticker.DeleteIconEvent;
import com.xiaopo.flying.sticker.DrawableSticker;
import com.xiaopo.flying.sticker.Sticker;
import com.xiaopo.flying.sticker.StickerView;
import com.xiaopo.flying.sticker.ZoomIconEvent;

import java.util.Arrays;

public class EditPhotoAct extends BaseActivity implements WaterMarkViewChangeListener {

    ActivityDmEditPhotoBinding binding;
    MediaData data;
    Sticker mSticker = null;
    BaseMark mMark = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDmEditPhotoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.ivBack.setOnClickListener(v -> finish());
        if (!getIntent().hasExtra("data")) {
            finish();
            return;
        }
        data = getIntent().getParcelableExtra("data");

        Glide.with(this).setDefaultRequestOptions(new RequestOptions()).load(data.getPath())
//                    .override(data.getWidth(), data.getHeight())
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(binding.ivPhoto);


//        try {
//            BaseMark mark = (BaseMark) Class.forName(Mark1.class.getName()).getDeclaredConstructor(
//                    Context.class,
//                    AttributeSet.class
//            ).newInstance(this, null);
//            Bitmap bm = Utils.convertViewToBitmap2(mark);
//            DrawableSticker sticker = new DrawableSticker(new BitmapDrawable(bm));
//            binding.stickerView.addSticker(sticker);
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }

        //currently you can config your own icons and icon event
        //the event you can custom
        BitmapStickerIcon deleteIcon = new BitmapStickerIcon(ContextCompat.getDrawable(this, com.xiaopo.flying.sticker.R.drawable.sticker_ic_close_white_18dp), BitmapStickerIcon.LEFT_TOP);
        deleteIcon.setIconEvent(new DeleteIconEvent());

        BitmapStickerIcon zoomIcon = new BitmapStickerIcon(ContextCompat.getDrawable(this, com.xiaopo.flying.sticker.R.drawable.sticker_ic_scale_white_18dp), BitmapStickerIcon.RIGHT_BOTOM);
        zoomIcon.setIconEvent(new ZoomIconEvent());

        binding.stickerView.setIcons(Arrays.asList(deleteIcon, zoomIcon));
        binding.stickerView.setOnStickerOperationListener(new StickerView.OnStickerOperationListener() {
            @Override
            public void onStickerAdded(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerClicked(@NonNull Sticker sticker) {
                if (mMark == null) {
                    return;
                }
                Intent i = new Intent(EditPhotoAct.this, EditMarkActivity.class);
                i.putExtra("name", mMark.getClass().getName());
                i.putExtra("from", 1);
                startActivityForResult(i, 10);
            }

            @Override
            public void onStickerDeleted(@NonNull Sticker sticker) {
                mMark = null;
                mSticker = null;
            }

            @Override
            public void onStickerDragFinished(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerTouchedDown(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerZoomFinished(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerFlipped(@NonNull Sticker sticker) {

            }

            @Override
            public void onStickerDoubleTapped(@NonNull Sticker sticker) {

            }
        });

        binding.ivSave.setOnClickListener(v -> save());
        binding.tvWatermark.setOnClickListener(v -> chooseWaterMark());
        binding.tvAddMoreMark.setOnClickListener(v -> {
            Intent i = new Intent(this, BatchPhotoAct.class);
            startActivity(i);
        });

        chooseWaterMark();
    }

    private void save() {
        binding.stickerView.setIcons(Arrays.asList());

//        for (BitmapStickerIcon item : binding.stickerView.getIcons()) {
//            item.setAlpha(0);
//        }
        Bitmap bm = Utils.convertViewToBitmap(binding.stickerView);
        SaveUtils.saveBitmapToAlbum(this, bm);
        ToastUtil.showShortToast(this, "已保存");
        setResult(RESULT_OK);
        finish();
    }

    private void chooseWaterMark() {
        MarkDlg dlg = new MarkDlg();
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {

            }
        });
        dlg.show(getSupportFragmentManager(), "");
    }

    @Override
    public void onWaterMarkViewChange(BaseMark mark) {
        if (mSticker != null) {
            binding.stickerView.remove(mSticker);
        }
        if (mark != null) {
            mark.destroyDrawingCache();
            Bitmap bm;
            if (mark.isFullScreen()) {
                bm = Utils.convertViewToBitmap(mark, binding.stickerView.getWidth(), binding.stickerView.getHeight());
            } else if (mark.isFullWidth()) {
                bm = Utils.convertViewToBitmap(mark, binding.stickerView.getWidth());
            } else {
                bm = Utils.convertViewToBitmap2(mark);
            }
            mSticker = new DrawableSticker(new BitmapDrawable(bm));
            if (mark.isFullScreen()) {
                binding.stickerView.addSticker(mSticker, 2f);
            } else {
                binding.stickerView.addSticker(mSticker);
            }
        }
        this.mMark = mark;
    }

    @Override
    public boolean needScale() {
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10) {//editMarkActivity
            if (resultCode == Activity.RESULT_OK) {
                mMark.update();
                onWaterMarkViewChange(mMark);
            }
        }
    }

    @Override
    protected boolean paddingTop() {
        return false;
    }
}
